﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace Lemonc.Tools
{
    class MD5Tools
    {
        private  static String SPECIAL_CODE = "!A%&*1#@z?";

        public static string Lower32(string s)
        {
            s = FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
            return s.ToLower();
 
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="plaintext">明文</param>
        /// <param name="bytesEncoding">编码</param>
        /// <returns></returns>
        public static string MD5Encrypt(string plaintext, Encoding bytesEncoding)
        {
            byte[] sourceBytes = bytesEncoding.GetBytes(plaintext);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hashedBytes = md5.ComputeHash(sourceBytes);
            return BitConverter.ToString(hashedBytes).Replace("-", "");
        }
        /// <summary>
        /// 以UTF8编码做MD5加密
        /// </summary>
        /// <param name="plaintext">明文</param>
        /// <returns></returns>
        public static string MD5Encrypt(string plaintext)
        {
            return MD5Encrypt(plaintext, Encoding.UTF8);
        }
        
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="Message"></param>
        /// <returns></returns>
        public static string Base64Code(string Message, Encoding endoding)
        {
            byte[] bytes = endoding.GetBytes(Message);
            return Convert.ToBase64String(bytes);
        }
        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="Message"></param>
        /// <returns></returns>
        public static string Base64Decode(string Message, Encoding endoding)
        {
            byte[] bytes = Convert.FromBase64String(Message);
            return endoding.GetString(bytes);
        }
    }
}
